import type { NextApiRequest, NextApiResponse } from 'next';
import { BeError, BeSuccess } from '@/utils/response';
import { dingUser } from '@/services/dingUserInfo';
import { setCookie } from '@/utils/Auth/utils';
import { USER_TOKEN, maxAge } from '@/utils/Auth/constants';
import { packingResult } from '@/utils/packingResult';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method?.toLocaleLowerCase() !== 'post') {
    res.status(200).json(BeError('不支持请求类型'));
    res.end();
    return;
  }
  const { body } = req;
  try {
    const result: any = await dingUser({ ...body });
    setCookie(res, USER_TOKEN, result?.data?.token, { maxAge });
    res.status(200).json(packingResult(result));
  } catch (error: any) {
    res.status(500).json(BeError(error));
  }
}
